package com.muse.aaa;

import java.util.concurrent.TimeUnit;

import lombok.AllArgsConstructor;
import lombok.Data;

public class NodeThreadTest {
    private LinkedNode node_c, node_b, node_a;

    public static void main(String[] args) {
        NodeThreadTest ntt = new NodeThreadTest();
        ntt.init();
        ntt.execute();
    }

    private void execute() {
        /** 开启线程Thread-A，试图改变链表中节点顺序为：nodeC——>nodeB——>nodeA */
        new Thread(()-> {
            // 执行链表头插变更操作
            LinkedNode temp = node_a, head = null, next;
            while (temp != null) {
                try {
                    next = temp.next;
                    temp.next = head;
                    head = temp;
                    temp = next;
                    TimeUnit.MILLISECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-A").start();

        /** 开启线程Thread-B */
        new Thread(()-> {
            LinkedNode temp = node_c;
            int i = 0;
            while (i < 10000) {
                i++;
            }
            System.out.println(temp);
        }, "Thread-B").start();
    }

    /**
     * 初始化链表中节点顺序：nodeA——>nodeB——>nodeC
     */
    private void init() {
        node_c = new LinkedNode("nodeC", null);
        node_b = new LinkedNode("nodeB", node_c);
        node_a = new LinkedNode("nodeA", node_b);
    }
}

@Data
@AllArgsConstructor
class LinkedNode{
    public String item;
    public LinkedNode next;
}
